<?php

namespace Plugin\ShowPointAmount4;

use Eccube\Entity\Product;
use Eccube\Entity\ProductClass;
use Eccube\Event\TemplateEvent;
use Eccube\Repository\BaseInfoRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class Event implements EventSubscriberInterface
{
    /**
     * @var BaseInfoRepository
     */
    private $baseInfoRepository;

    /**
     * @return array
     */
    public static function getSubscribedEvents()
    {
        return [
            'Product/detail.twig' => 'onProductTwig'
        ];
    }


    public function __construct(
        BaseInfoRepository $baseInfoRepository
    )
    {
        $this->baseInfoRepository = $baseInfoRepository;
    }


    public function onProductTwig(TemplateEvent $event)
    {
        $BaseInfo = $this->baseInfoRepository->get();

        if (!$BaseInfo->isOptionPoint()) {
            return;
        }

        $basicPointRate = $BaseInfo->getBasicPointRate();

        $Product = $event->getParameter('Product');
        $json = $this->getClassCategoriesAsJson($Product);

        $points = [];
        /* @var $productClass ProductClass */
        foreach ($Product->getProductClasses() as $ProductClass) {
            if (!is_null($ProductClass->getPointRate())) {
                $rate = $ProductClass->getPointRate();
            } else {
                $rate = $basicPointRate;
            }

            if ( $rate !== null) {
                $points[] = round($ProductClass->getPrice02() * ($rate / 100)) * 1;
            }
        }

        $event->setParameter('point_min', min($points));
        $event->setParameter('point_max', max($points));

        $event->setParameter('point_json', $json);
        $event->addSnippet('@ShowPointAmount4/product_detail_point.twig');
        $event->addSnippet('@ShowPointAmount4/product_detail_point_js.twig');

    }



    /**
     * Get the ClassCategories as JSON.
     *
     * @param Product $Product
     *
     * @return string
     */
    public function getClassCategoriesAsJson(Product $Product)
    {
        $Product->_calc();
        $class_categories = [
            '__unselected' => [
                '__unselected' => [
                    'name' => trans('common.select'),
                    'product_class_id' => '',
                ],
            ],
        ];

        $BaseInfo = $this->baseInfoRepository->get();
        $basicPointRate = $BaseInfo->getBasicPointRate();

        foreach ($Product->getProductClasses() as $ProductClass) {
            /** @var ProductClass $ProductClass */
            if (!$ProductClass->isVisible()) {
                continue;
            }

            if (!is_null($ProductClass->getPointRate())) {
                $rate = $ProductClass->getPointRate();
            } else {
                $rate = $basicPointRate;
            }

            /* @var $ProductClass \Eccube\Entity\ProductClass */
            $ClassCategory1 = $ProductClass->getClassCategory1();
            $ClassCategory2 = $ProductClass->getClassCategory2();
            if ($ClassCategory2 && !$ClassCategory2->isVisible()) {
                continue;
            }
            $class_category_id1 = $ClassCategory1 ? (string) $ClassCategory1->getId() : '__unselected2';
            $class_category_id2 = $ClassCategory2 ? (string) $ClassCategory2->getId() : '';

            if ( $rate === null) {
                $point = '';
            } else {
                $point = round($ProductClass->getPrice02() * ($rate / 100)) * 1;
            }

            $class_categories[$class_category_id1]['#'] = [
                'classcategory_id2' => '',
                'name' => trans('common.select'),
                'product_class_id' => '',
            ];
            $class_categories[$class_category_id1]['#'.$class_category_id2] = [
                'point' => $point,
            ];
        }

        return json_encode($class_categories);
    }
}
